/*     */ package org.yaml.snakeyamla.configuration.file;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.Reader;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.logging.Level;
/*     */ import org.apache.commons.lang3.Validate;
/*     */ import org.yaml.snakeyamla.DumperOptions;
/*     */ import org.yaml.snakeyamla.Yaml;
/*     */ import org.yaml.snakeyamla.configuration.Configuration;
/*     */ import org.yaml.snakeyamla.configuration.ConfigurationOptions;
/*     */ import org.yaml.snakeyamla.configuration.ConfigurationSection;
/*     */ import org.yaml.snakeyamla.configuration.InvalidConfigurationException;
/*     */ import org.yaml.snakeyamla.configuration.MemoryConfigurationOptions;
/*     */ import org.yaml.snakeyamla.constructor.BaseConstructor;
/*     */ import org.yaml.snakeyamla.error.YAMLException;
/*     */ import org.yaml.snakeyamla.representer.Representer;
/*     */ 
/*     */ public class YamlConfiguration extends FileConfiguration {
/*  23 */   private String loadFromString = "";
/*     */   
/*     */   protected static final String COMMENT_PREFIX = "# ";
/*     */   
/*     */   protected static final String BLANK_CONFIG = "{}\n";
/*  28 */   private final DumperOptions yamlOptions = new DumperOptions();
/*  29 */   private final Representer yamlRepresenter = new YamlRepresenter();
/*  30 */   private final Yaml yaml = new Yaml((BaseConstructor)new YamlConstructor(), this.yamlRepresenter, this.yamlOptions);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public YamlConfiguration(String fileName) {
/*  37 */     setFileName(fileName);
/*     */   }
/*     */ 
/*     */   
/*     */   public String saveToString() {
/*  42 */     this.yamlOptions.setIndent(options().indent());
/*  43 */     this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
/*  44 */     this.yamlOptions.setAllowUnicode(SYSTEM_UTF);
/*  45 */     this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
/*     */     
/*  47 */     String header = buildHeader();
/*  48 */     String dump = this.yaml.dump(getValues(false));
/*     */     
/*  50 */     if (dump.equals("{}\n")) {
/*  51 */       dump = "";
/*     */     }
/*     */     
/*  54 */     return header + dump;
/*     */   }
/*     */   
/*     */   public void loadFromString(String contents) throws InvalidConfigurationException {
/*     */     Map<?, ?> input;
/*  59 */     Validate.notNull(contents, "Contents cannot be null", new Object[0]);
/*     */ 
/*     */     
/*     */     try {
/*  63 */       input = (Map<?, ?>)this.yaml.load(contents);
/*  64 */       this.loadFromString = contents;
/*  65 */     } catch (YAMLException e) {
/*  66 */       throw new InvalidConfigurationException(e);
/*  67 */     } catch (ClassCastException e) {
/*  68 */       throw new InvalidConfigurationException("Top level is not a Map.");
/*     */     } 
/*     */     
/*  71 */     String header = parseHeader(contents);
/*  72 */     if (header.length() > 0) {
/*  73 */       options().header(header);
/*     */     }
/*     */     
/*  76 */     if (input != null)
/*  77 */       convertMapsToSections(input, (ConfigurationSection)this); 
/*     */   }
/*     */   
/*     */   public void loadFromStringIgnoreException(String contents) {
/*     */     Map<?, ?> input;
/*  82 */     Validate.notNull(contents, "Contents cannot be null", new Object[0]);
/*     */ 
/*     */     
/*     */     try {
/*  86 */       input = (Map<?, ?>)this.yaml.load(contents);
/*  87 */       this.loadFromString = contents;
/*  88 */     } catch (YAMLException|ClassCastException e) {
/*  89 */       e.printStackTrace();
/*     */       
/*     */       return;
/*     */     } 
/*  93 */     String header = parseHeader(contents);
/*  94 */     if (header.length() > 0) {
/*  95 */       options().header(header);
/*     */     }
/*     */     
/*  98 */     if (input != null) {
/*  99 */       convertMapsToSections(input, (ConfigurationSection)this);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void convertMapsToSections(Map<?, ?> input, ConfigurationSection section) {
/* 104 */     for (Map.Entry<?, ?> entry : input.entrySet()) {
/* 105 */       String key = entry.getKey().toString();
/* 106 */       Object value = entry.getValue();
/*     */       
/* 108 */       if (value instanceof Map) {
/* 109 */         convertMapsToSections((Map<?, ?>)value, section.createSection(key)); continue;
/*     */       } 
/* 111 */       section.set(key, value);
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   protected String parseHeader(String input) {
/* 117 */     String[] lines = input.split("\r?\n", -1);
/* 118 */     StringBuilder result = new StringBuilder();
/* 119 */     boolean readingHeader = true;
/* 120 */     boolean foundHeader = false;
/*     */     
/* 122 */     for (int i = 0; i < lines.length && readingHeader; i++) {
/* 123 */       String line = lines[i];
/*     */       
/* 125 */       if (line.startsWith("# ")) {
/* 126 */         if (i > 0) {
/* 127 */           result.append("\n");
/*     */         }
/*     */         
/* 130 */         if (line.length() > "# ".length()) {
/* 131 */           result.append(line.substring("# ".length()));
/*     */         }
/*     */         
/* 134 */         foundHeader = true;
/* 135 */       } else if (foundHeader && line.length() == 0) {
/* 136 */         result.append("\n");
/* 137 */       } else if (foundHeader) {
/* 138 */         readingHeader = false;
/*     */       } 
/*     */     } 
/*     */     
/* 142 */     return result.toString();
/*     */   }
/*     */ 
/*     */   
/*     */   protected String buildHeader() {
/* 147 */     String header = options().header();
/*     */     
/* 149 */     if (options().copyHeader()) {
/* 150 */       Configuration def = getDefaults();
/*     */       
/* 152 */       if (def != null && def instanceof FileConfiguration) {
/* 153 */         FileConfiguration filedefaults = (FileConfiguration)def;
/* 154 */         String defaultsHeader = filedefaults.buildHeader();
/*     */         
/* 156 */         if (defaultsHeader != null && defaultsHeader.length() > 0) {
/* 157 */           return defaultsHeader;
/*     */         }
/*     */       } 
/*     */     } 
/*     */     
/* 162 */     if (header == null) {
/* 163 */       return "";
/*     */     }
/*     */     
/* 166 */     StringBuilder builder = new StringBuilder();
/* 167 */     String[] lines = header.split("\r?\n", -1);
/* 168 */     boolean startedHeader = false;
/*     */     
/* 170 */     for (int i = lines.length - 1; i >= 0; i--) {
/* 171 */       builder.insert(0, "\n");
/*     */       
/* 173 */       if (startedHeader || lines[i].length() != 0) {
/* 174 */         builder.insert(0, lines[i]);
/* 175 */         builder.insert(0, "# ");
/* 176 */         startedHeader = true;
/*     */       } 
/*     */     } 
/*     */     
/* 180 */     return builder.toString();
/*     */   }
/*     */ 
/*     */   
/*     */   public YamlConfigurationOptions options() {
/* 185 */     if (this.options == null) {
/* 186 */       this.options = new YamlConfigurationOptions(this);
/*     */     }
/*     */     
/* 189 */     return (YamlConfigurationOptions)this.options;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static YamlConfiguration loadConfiguration(File file) {
/* 206 */     Validate.notNull(file, "File cannot be null", new Object[0]);
/*     */     
/* 208 */     YamlConfiguration config = new YamlConfiguration();
/*     */ 
/*     */     
/* 211 */     try { config.load(file); }
/* 212 */     catch (FileNotFoundException fileNotFoundException) {  }
/* 213 */     catch (IOException|InvalidConfigurationException ex)
/* 214 */     { log(Level.SEVERE, "Cannot load " + file, ex); }
/*     */ 
/*     */     
/* 217 */     return config;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @Deprecated
/*     */   public static YamlConfiguration loadConfiguration(InputStream stream) {
/* 236 */     Validate.notNull(stream, "Stream cannot be null", new Object[0]);
/*     */     
/* 238 */     YamlConfiguration config = new YamlConfiguration();
/*     */     
/*     */     try {
/* 241 */       config.load(stream);
/* 242 */     } catch (IOException|InvalidConfigurationException ex) {
/* 243 */       log(Level.SEVERE, "Cannot load configuration from stream", ex);
/*     */     } 
/*     */     
/* 246 */     return config;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static YamlConfiguration loadConfiguration(Reader reader) {
/* 262 */     Validate.notNull(reader, "Stream cannot be null", new Object[0]);
/*     */     
/* 264 */     YamlConfiguration config = new YamlConfiguration();
/*     */     
/*     */     try {
/* 267 */       config.load(reader);
/* 268 */     } catch (IOException ex) {
/* 269 */       log(Level.SEVERE, "Cannot load configuration from stream", ex);
/* 270 */     } catch (InvalidConfigurationException ex) {
/* 271 */       log(Level.SEVERE, "Cannot load configuration from stream", (Exception)ex);
/*     */     } 
/*     */     
/* 274 */     return config;
/*     */   }
/*     */ 
/*     */   
/*     */   public boolean isColor(String path) {
/* 279 */     return false;
/*     */   }
/*     */ 
/*     */   
/*     */   public static void log(Level level, String str, Exception ex) {
/* 284 */     ex.printStackTrace();
/*     */   }
/*     */ 
/*     */   
/*     */   public YamlConfiguration reloadFromString() {
/* 289 */     YamlConfiguration yaml = new YamlConfiguration();
/* 290 */     yaml.setFileName(getFileName());
/* 291 */     yaml.loadFromStringIgnoreException(this.loadFromString);
/*     */     
/* 293 */     Set<String> keys = yaml.getKeys(true);
/* 294 */     for (String key : keys) {
/* 295 */       if (yaml.isConfigurationSection(key)) {
/* 296 */         yaml.getConfigurationSection(key).setFileName(getFileName());
/*     */       }
/*     */     } 
/* 299 */     return yaml;
/*     */   }
/*     */   
/*     */   public YamlConfiguration() {}
/*     */ }


/* Location:              D:\fanbian\[D][龙之核心]DragonCore-2.6.0.3.jar!\org\yaml\snakeyamla\configuration\file\YamlConfiguration.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */